﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Chinesys.Entities;
using Chinesys.Domain;
using Chinesys.UI.Helpers;

namespace Chinesys.UI
{
    public abstract class PageBase : Page
    {
        public UserEntity User
        {
            get
            {
                return SecurityHelper.GetLoggedUser(Context.User.Identity.Name);                
            }
        }

        protected void CheckUserInRole(params string[] roles)
        {
            if (User == null)
                Response.Redirect("~/Account/Login.aspx?error=notLogged");

            bool isInRole = false;

            foreach (string role in roles)
            {
                if (User.Role.RoleName == role)
                    isInRole = true;
            }

            if (!isInRole)
                Response.Redirect("~/Account/Login.aspx?error=invalidRole");
        }
    }
}
